package com.atguigu.gulimall.product.web;

import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.service.CategoryService;
import com.atguigu.gulimall.product.vo.Catalog2Vo;
import org.redisson.api.RLock;
import org.redisson.api.RReadWriteLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
public class IndexController {
    @Autowired
    CategoryService categoryService;

    @Autowired
    RedissonClient redisson;

    @Autowired
    StringRedisTemplate redisTemplate;

    @GetMapping({"/", "/index.html"})
    public String indexPage(Model model) {
        List<CategoryEntity> categorys = categoryService.getLevel1Categorys();
        model.addAttribute("categorys", categorys);
        return "index";
    }

    @ResponseBody
    @GetMapping("index/catalog.json")
    public Map<String, List<Catalog2Vo>> getCatalogJson() {
        Map<String, List<Catalog2Vo>> map = categoryService.getCatalogJson();
        return map;
    }

    @GetMapping("hello")
    @ResponseBody
    public String hello() {
        RLock lock = redisson.getLock("my-lock");
        try {
            lock.lock();
            System.out.println("获取锁成功！");
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
            System.out.println("释放锁成功！");
        }

        return "hello";
    }

    @GetMapping("write")
    @ResponseBody
    public String write() {
        String uuid = UUID.randomUUID().toString();
        RLock lock = redisson.getReadWriteLock("rw-lock").writeLock();
        System.out.println("获取写锁" + Thread.currentThread().getId());
        lock.lock();
        System.out.println("加写锁" + Thread.currentThread().getId());
        redisTemplate.opsForValue().set("writeValue", uuid);
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
            System.out.println("释放写锁" + Thread.currentThread().getId());
        }


        return uuid;
    }

    @GetMapping("read")
    @ResponseBody
    public String read() {
        RLock lock = redisson.getReadWriteLock("rw-lock").readLock();
        System.out.println("获取读锁" + Thread.currentThread().getId());
        lock.lock();
        System.out.println("加读锁" + Thread.currentThread().getId());
        String s = redisTemplate.opsForValue().get("writeValue");
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
            System.out.println("释放读锁" + Thread.currentThread().getId());
        }


        return s;
    }

}
